<?php

namespace Lib\PayApi\Baifupass;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/22
 * Time: 1:36
 */
class PaymentClient {
    public static function pay($orderNo, $money, $pbank) {
        $baifupass = C('BAIFUPASS');
        $banks = explode('_',$pbank);

        $arr['appid'] = $baifupass['appid'];//appid//找管理员
        $arr['amount'] = $money;//单位元 个位与十位不能相同不能为0 比如 10 11 都不行
        $arr['down_trade_no'] = $orderNo;//订单号
        $arr['goodsname'] = "无";//备注
        $arr['backurl'] = $baifupass['notifyUrl'];//回调地址
        $url = 'unionPayBaseUri';
        if ($banks[0] != "UNIONPAY") {
            $arr['trxType'] = $baifupass['tongdao'][$banks[0]][$banks[1]];//支付形态 微信扫码  WX_SCANCODE  微信收银台  WX_SCANCODE_JSAPI 阿里扫码  Alipay_SCANCODE   阿里收银台 Alipay_SCANCODE_JSAPI
            $url = 'baseUri';
        }
        $str = self::TosignHttp($arr);
        $sign = self::EnTosignHP($str, $baifupass['signkey']);
        $arr['sign']=$sign;//签名

        // var_dump($arr);
        return self::http_request($baifupass[$url], $arr);
        // return  self::http_request_json($baifupass['baseUri'], $arr); //Utils::post($baifupass['baseUri'], $arr);
    }

    public static function query($orderNo) {
        /*$zito = C('ZITO');
        $requestarray = array(
            "id" => $zito['merchantId'],
            "appid" => $zito['appid'],
            "key" => $zito['key'],
        );

        $md5str = "";
        foreach ($requestarray as $key => $val) {
            $md5str = $md5str . $val ;
        }
        $sign = md5($md5str);

        $requestarray["posttime"] = self::udate("YmdHiZ");
        $requestarray["orderId"] = $orderNo;//date("YmdHis");
        $requestarray["sign"] = $sign;
        return Utils::post($zito['findUri'], $requestarray);*/
    }

    public static function http_request($url,$post_data){
        $ch = curl_init();//打开
        $Pt=http_build_query($post_data);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$Pt);
        //echo $post_data;
        //exit;
        $response  = curl_exec($ch);
        curl_close($ch);//关闭
        //pr($response);
        //ECHO $response;
        // $result = json_decode($response,true);
        return $response;
    }

    /**
     * http数组拼接 协议使用
     * @param string $ar 解密内容
     * @return string 字符串类型的返回结果
     */
    public static function TosignHttp($ar) {
        ksort($ar);
        //pr($ar);
        //exit;
        //$arr=ArrEncode($ar);
        //$json=json_encode($arr);

        //$json=urldecode($json);
        $httpcontent="";
        foreach($ar as $key=>$val) {
            if($httpcontent == "") {
                $httpcontent="#";
            } else {
                $httpcontent.="#";
            }
            $httpcontent.=$ar[$key];
        }

        return $httpcontent;
    }

    /**
     * http加密 协议使用
     * @param string $string 解密内容
     * @param string $signkey 秘钥
     * @return string 字符串类型的返回结果
     */

    public static function EnTosignHP($string, $signkey) {
        $nd=$string."#".$signkey;
        $string=md5($nd);
        //echo "加密前:".$nd."<br>";
        //echo $nd;
        //exit;
        return strtoupper($string);
    }
}